﻿using App.Extension;
using Simple.Framework.Logger;
using Simple.Framework.Schedule;
using Simple.PrettyNet;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

/*********************************************************
 * 命名空间 App.Schedule
 * 接口名称 CheckCommandSchedule
 * 开发人员：11920
 * 创建时间：2023/4/17 14:31:37
 * 描述说明：
 * 更改历史：
 * 
 * *******************************************************/
namespace App.Schedule
{
	internal class CheckCommandSchedule:ISchedule
	{
		private ILogger logger;
		public CheckCommandSchedule(IClientManager clientManager, ILogger logger)
		{
			ScheduleInfo = new ScheduleInfo();
			ScheduleInfo.Period = 10000;
			this.clientManager = clientManager;
			this.logger = logger;
		}
		

		public ScheduleInfo ScheduleInfo { get { return scheduleInfo; } set { scheduleInfo = value; } }

		public void Run()
		{
			foreach (var item in clientManager.GetClients())
			{
				if (item.Context is IDTContext idtContext)
				{
					foreach (var command in idtContext.Dic_commands)
					{
						if (command.Value.status==1)
						{
							idtContext.Dic_commands.Remove(command.Key);
							
						}
						else
						{
							item.Context.Send(command.Value.buffer);
							logger.Warn($"commandId:{command.Key} 已经重复{command.Value.resendCount}次!");
						}
					}
				}
			}
		}

		private IClientManager clientManager;
		private ScheduleInfo scheduleInfo;
	}
}
